<template>
  <div class="login-container">
    <!-- 左边登录界面 -->
    <div class="left">
      <!-- 标题 -->
      <div class="title-box">
        <img src="@/assets/img/layout_icon.png" alt="" /><span class="title"
          >黑马面面</span
        ><span class="line"></span><span class="sub-title">用户登录</span>
      </div>
      <!-- form表单部分 -->
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="login-form"
      >
        <!-- 手机号码 -->
        <el-form-item prop="phone">
          <el-input
            prefix-icon="el-icon-user"
            v-model="ruleForm.phone"
            placeholder="请输入手机号"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            prefix-icon="el-icon-lock"
            show-password
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- 验证码 -->
        <el-row :gutter="20">
          <!-- 输入验证码文本框 -->
          <el-col :span="16">
            <el-form-item prop="code">
              <el-input
                v-model="ruleForm.code"
                placeholder="请输入验证码"
                prefix-icon="el-icon-key"
              ></el-input>
            </el-form-item>
          </el-col>
          <!-- 验证码图片 -->
          <el-col :span="8">
            <img class="captcha" :src="codeURL" @click="getCode" />
          </el-col>
        </el-row>
        <!-- 用户同意协议 -->
        <el-form-item prop="isCheck">
          <el-checkbox v-model="ruleForm.isCheck"></el-checkbox
          >&emsp;我已阅读并同意<el-link
            href="http://www.baidu.com"
            type="primary"
            >用户协议</el-link
          >和<el-link href="http://www.baidu.com" type="primary"
            >隐私条款</el-link
          >
        </el-form-item>
        <!-- 登录按钮 -->
        <el-form-item>
          <el-button type="primary" style="width:100%" @click="loginClick"
            >登录</el-button
          >
        </el-form-item>
        <!-- 注册按钮 -->
        <el-form-item>
          <el-button type="primary" style="width:100%" @click="register"
            >注册</el-button
          >
        </el-form-item>
        <!-- 注册页面 -->
      </el-form>
    </div>
    <!-- 右边界面图片 -->
    <div class="right"><img src="@/assets/img/login_bg.png" alt="" /></div>
    <!-- 子组件注册页面 -->
    <register ref="registerRef"></register>
  </div>
</template>
<script>
// 按需导入
import { saveToken } from '@/utils/token'
// 导入子组件
import register from './register'
export default {
  name: 'Login',
  data () {
    return {
      codeURL: process.env.VUE_APP_BASEURL + '/captcha?type=login',
      ruleForm: {
        phone: '18814444921', // 手机号
        password: 'my188921', // 密码
        code: '', // 验证码
        isCheck: false // 是否勾选了用户协议
      },
      //   校验规则
      rules: {
        // 手机号
        phone: [
          //   { required: true, message: '手机号码不能为空', trigger: 'blur' },
          //   { min: 11, max: 11, message: '手机号码格式不正确', trigger: 'blur' }
          {
            validator: (rule, value, callback) => {
              if (!value) {
                callback(new Error('手机号不能为空'))
                return
              }
              const reg = /^1[3-9][0-9]{9}$/
              if (!reg.test(value)) {
                return callback(new Error('手机号不合法'))
              }
              callback()
            },
            trigger: 'blur'
          }
        ],
        // 密码
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 12, message: '密码不能低于6到12位', trigger: 'blur' }
        ],
        // 验证码
        code: [
          { required: true, message: '验证码不能为空', trigger: 'blur' },
          { min: 4, max: 4, message: '验证码必须是4位', trigger: 'blur' }
        ],
        // 用户协议和隐私条款
        isCheck: [
          {
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error('必须勾选用户协议'))
              }
              callback()
            },
            trigger: 'change'
          }
        ]
      }
    }
  },
  // 注册子组件
  components: {
    register
  },
  methods: {
    // 获取验证码
    getCode () {
      this.codeURL =
        process.env.VUE_APP_BASEURL +
        '/captcha?type=login&t=' +
        (new Date() - 0)
    },
    // 登录
    loginClick () {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) return
        // 发送登录请求
        const res = await this.$axios.post('/login', this.ruleForm)
        // console.log(res)
        if (res.code === 200) {
          // 提示
          this.$message({
            message: '登录成功',
            type: 'success'
          })
          // 保存token
          saveToken(res.data.token)
          // 跳转到后台页面
          this.$router.push('/layout')
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 注册
    register () {
      this.$refs.registerRef.centerDialogVisible = true
    }
  }
}
</script>

<style lang="less">
.login-container {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  background: linear-gradient(225deg, #1493fa, #01c6fa);
  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 48px;
    .title-box {
      display: flex;
      align-items: center;
      img {
        width: 22px;
        height: 17px;
        margin-right: 16px;
      }
      .title {
        font-size: 24px;
        margin-right: 14px;
      }
      .line {
        width: 1px;
        background-color: #c7c7c7;
        height: 27px;
        margin-right: 12px;
      }
      .sub-title {
        font-size: 22px;
      }
    }
    .login-form {
      margin-top: 29px;
      .captcha {
        width: 100%;
        height: 40px;
      }
    }
  }
}
</style>
